package com.bookmall.member.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bookmall.common.utils.MyQuery;
import com.bookmall.common.utils.PageUtils;
import com.bookmall.member.domain.MemberInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.bookmall.member.domain.MemberReceiveAddress;
import com.bookmall.member.service.IMemberReceiveAddressService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 用户收货地址Controller
 *
 * @author huyulu
 * @date 2022-04-21
 */
@RestController
@RequestMapping("/member/address")
public class MemberReceiveAddressController extends BaseController
{
    @Autowired
    private IMemberReceiveAddressService memberReceiveAddressService;

    /**
     * 获取会员的所有地址列表
     * @author 胡菜鸡
     * @date 2022/4/21 17:43
     */
    @GetMapping("/address/{memberId}")
    public List<MemberReceiveAddress> getAddress(@PathVariable("memberId") String memberId){
        return memberReceiveAddressService.getAddress(memberId);
    }

    /**
     * 查询用户收货地址列表
     */
    @PreAuthorize("@ss.hasPermi('member:address:list')")
    @GetMapping("/list")
    public AjaxResult list(MemberReceiveAddress memberReceiveAddress)
    {
        IPage<MemberReceiveAddress> page = memberReceiveAddressService.page(new MyQuery<MemberReceiveAddress>().getPage(memberReceiveAddress), new QueryWrapper<>(memberReceiveAddress));
        PageUtils pageUtils = new PageUtils(page);
        return AjaxResult.success(pageUtils);
    }

    /**
     * 导出用户收货地址列表
     */
    @PreAuthorize("@ss.hasPermi('member:address:export')")
    @Log(title = "用户收货地址", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MemberReceiveAddress memberReceiveAddress)
    {
        List<MemberReceiveAddress> list = memberReceiveAddressService.list(new QueryWrapper<>(memberReceiveAddress));
        ExcelUtil<MemberReceiveAddress> util = new ExcelUtil<MemberReceiveAddress>(MemberReceiveAddress.class);
        util.exportExcel(response, list, "用户收货地址数据");
    }

    /**
     * 获取用户收货地址详细信息
     */
    @PreAuthorize("@ss.hasPermi('member:address:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success(memberReceiveAddressService.getById(id));
    }

    /**
     * 新增用户收货地址
     */
    @PreAuthorize("@ss.hasPermi('member:address:add')")
    @Log(title = "用户收货地址", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MemberReceiveAddress memberReceiveAddress)
    {
        return toAjax(memberReceiveAddressService.insertNewAddress(memberReceiveAddress));
    }

    /**
     * 修改用户收货地址
     */
    @PreAuthorize("@ss.hasPermi('member:address:edit')")
    @Log(title = "用户收货地址", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MemberReceiveAddress memberReceiveAddress)
    {
        return toAjax(memberReceiveAddressService.updateById(memberReceiveAddress));
    }

    /**
     * 删除用户收货地址
     */
    @PreAuthorize("@ss.hasPermi('member:address:remove')")
    @Log(title = "用户收货地址", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<String> ids)
    {
        return toAjax(memberReceiveAddressService.removeBatchByIds(ids));
    }
}
